<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <p>以下是模板：</p>
    <mabaoguo></mabaoguo>
    <mabaoguo></mabaoguo>
    <mabaoguo></mabaoguo>
    <hr>
    <wjl></wjl>
    <wjl></wjl>
    <wjl></wjl>
    <hr>
    <button @click="num ++">点击次数{{num}}</button>
    <button @click="num ++">点击次数{{num}}</button>
    <button @click="num ++">点击次数{{num}}</button>
    <hr>
    <btn></btn>
    <btn></btn>
    <btn></btn>
    <hr>
    <p>以下是组件的切换</p>
    <p>通过is属性，实现动态组件的功能。is属性的值就是要渲染的组件。</p>
    <keep-alive>
      <div :is="componentName"></div>
    </keep-alive>
    <button
      @click="componentName == 'mabaoguo'?componentName = 'btn':componentName = 'mabaoguo'"
    >切换组件</button>
  </div>
  <script src="./vue.js"></script>
  <script>
    // 全局注册
    Vue.component('wjl',{
      template:`
      <div>
        <p>定一个小目标，先挣他{{target}}</p>
        <p>一无所有</p>
      </div>`,
      data() {
        return {
          target: 100000000,
        }
      },
    })
    let vm = new Vue({
      el: '#app',
      data: {
        num: 1,
        componentName: 'mabaoguo'
      },
      // 局部注册组件
      components: {
        'mabaoguo': {
          // 模板，用来写组件中的内容
          template: `<mark @click="age ++">偷袭{{age}}岁的老同志，不讲武德</mark>`,
          data() {
            return {
              age: 69
            }
          },
        },
        'btn': {
          template:`<div>
          <button @click="num1 ++">点击次数{{num1}}</button><p>data必须是函数</p>
          </div>`,
          data() {
            return {
              num1: 1
            }
          },
        }
      }
    })
  </script>
</body>
</html>